# -*- coding: utf-8 -*-

import urllib2
import xml.etree.ElementTree as ElementTree

import logging

import html5lib
from html5lib import treebuilders, treewalkers, serializer

from onlinebrary.model.html_data import HtmlData
from onlinebrary.model.feed_data import FeedData

import onlinebrary.lib.identifier as identifier

log = logging.getLogger(__name__)




class HtmlDiscoverer(object):

        def __init__(self):
            pass
        
        def get_info(self, raw_url):
        
            if raw_url[:7] != 'http://':
                url = 'http://' + raw_url
            else:
                url = raw_url
                
        
            log.debug('Discovering url %s', url)
        
            info = HtmlData(url)
        
            p = html5lib.HTMLParser(tree=treebuilders.getTreeBuilder("etree", ElementTree))
            try:
                src = urllib2.urlopen(url)
            except Exception, e:
                log.error(e)
                return info

            info.encoding = None
            if src.headers.has_key('content-type'):
                try:
                    info.encoding = src.headers['content-type'].split('charset=')[1]
                    log.debug('For [%s] encoding was identified: %s', url, encoding)
                except Exception:
                    pass
            try:
                if info.encoding:
                    content = unicode(src.read(), info.encoding)
                else:
                    content = src.read()
                tree = p.parse(content)
            except Exception, e:
                log.error(e)
                return Exception('Ошибка при конвертации.', e)
            
            title_tag = tree.find(r'.//title')
            info.title = title_tag.text
            
            log.debug('HTML page title is %s', info.title)
            
            links = tree.findall(r'.//link')
            feed_links = dict()
            
            for link in links:
                if link.attrib.has_key('type') and link.attrib['type'] in identifier.FEED_TYPES:
                    if link.attrib.has_key('title'):
                        title = link.attrib['title']
                    else:
                        title = '(Без названия)'
                    href = link.attrib['href']
                    
                    if url.split('http://')[1] not in href:
                        href = url + '/' + href.strip('/')
                    
                    feed_links[title] = href
                    
            for title, link in feed_links.items():
                info.feeds.append(FeedData(link, title))
            log.debug('HTML data parsed')
            return info
        
        
